//
//  AllClassifyView.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/9/28.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "AllClassifyView.h"
#import "AllClassifyCollectionViewCell.h"


@interface AllClassifyView ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) UICollectionView *allClassColl;

@property (nonatomic, strong) NSMutableArray *allClassArray;

@end

@implementation AllClassifyView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupView];
        
        [self addConstraint];
        
        [self loadData];
    }
    return self;
}

- (void)setupView{
    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelEcent:)];
    
    self.bgView = [[UIView alloc]init];
    [self.bgView setBackgroundColor:[UIColor blackColor]];
    [self.bgView setAlpha:0.5];
    [self.bgView setUserInteractionEnabled:YES];
    [self.bgView addGestureRecognizer:gest];
    [self addSubview:self.bgView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    self.allClassColl = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
    [self.allClassColl setDataSource:self];
    [self.allClassColl setDelegate:self];
    [self.allClassColl setShowsVerticalScrollIndicator:NO];
    [self.allClassColl setShowsHorizontalScrollIndicator:NO];
    [self.allClassColl setBackgroundColor:[UIColor clearColor]];
    [self.allClassColl registerClass:[AllClassifyCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self addSubview:self.allClassColl];
}

- (void)addConstraint{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self.allClassColl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.height.equalTo(200);
    }];
}

- (void)loadData{
    NSArray *titleArray = [[NSArray alloc]initWithObjects:@"全部分类",@"干洗",@"买房",@"租房",@"苦力",@"丫丢丢",@"快递",@"家政",@"开锁",@"简历",@"蔬果",@"买车",@"洗车",@"秒杀",@"上户",@"出租房",@"同城快递", nil];
    for (int i = 0; i< titleArray.count; i ++) {
        AllClassIfyModel *model = [[AllClassIfyModel alloc]init];
        model.title = [titleArray objectAtIndex:i];
        if (i == 0) {
            model.isSel = YES;
        }else{
            model.isSel = NO;
        }
        model.value = [NSString stringWithFormat:@"%zd",i];
        [self.allClassArray addObject:model];
    }
    
    int value = self.allClassArray.count % (2);
    int count = self.allClassArray.count/2 + value;
    float height = 44 *count;
    if (height > ScreenHeight - 104) {
        [self.allClassColl setBounces:YES];
        height = ScreenHeight - 104;
    }else{
        [self.allClassColl setBounces:NO];
    }
    
    [self.allClassColl mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.height.equalTo(height);
    }];
    [self.allClassColl reloadData];
}

#pragma =========UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allClassArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    AllClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[AllClassifyCollectionViewCell alloc]init];
    }
    AllClassIfyModel *itemModel = [self.allClassArray objectAtIndex:indexPath.row];
    [cell setClassData:itemModel];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    AllClassIfyModel *itemModel = [self.allClassArray objectAtIndex:indexPath.row];
    for (int i = 0; i< self.allClassArray.count; i ++) {
        AllClassIfyModel *model = [self.allClassArray objectAtIndex:i];
        if (i == indexPath.row) {
            model.isSel = YES;
        }else{
            model.isSel = NO;
        }
    }
    [self.allClassColl reloadData];
    self.selectAction(itemModel);
}

#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(ScreenWidth/3, 44) ;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (void)cancelEcent:(UITapGestureRecognizer *)gest{
    self.cancelAction();
}

- (NSMutableArray *)allClassArray{
    if (!_allClassArray) {
        _allClassArray = [[NSMutableArray alloc]init];
    }
    return _allClassArray;
}

@end
